<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Cocoapods最佳实践 | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Cocoapods最佳实践</h1>
<p>对于cocoapods的最佳实践有这么几个问题需要回答：</p>
<ol>
<li>cocoapods使用过程中有什么问题</li>
<li>如何解决，基于什么原理</li>
</ol>
<h3 id="cocoapods使用过程中有什么问题">cocoapods使用过程中有什么问题？</h3>
<p>主要是遇到多人协作和多项目协作时，每个人的本地Ruby版本和cocoapod版本不一致，有可能会导致一些无法预料的问题，和沟通上的成本。比如：对同一个问题的描述，看到的问题不一样，原因是pod版本不同导致的。</p>
<h3 id="如何解决基于什么原理">如何解决，基于什么原理？</h3>
<p>cocoapods实际上是一个Ruby的第三方包，基于Ruby的包管理，我们可以管理cocoapods，然后我们基于cocoapods管理iOS上的第三方包。
使用 gem gvm 进行版本管理</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>brew install rbenv
</span></span><span style="display:flex;"><span>rbenv install 2.7.0 <span style="color:#75715e"># 安装ruby版本</span>
</span></span><span style="display:flex;"><span>rbenv shell 2.7.0 <span style="color:#75715e"># 使版本生效</span>
</span></span><span style="display:flex;"><span>gem pristine --all <span style="color:#75715e"># 切换ruby版本后 执行，不然有些库bundle找不到</span>
</span></span><span style="display:flex;"><span>ruby --version <span style="color:#75715e"># 检查版本</span>
</span></span><span style="display:flex;"><span>ruby 2.7.0p0 <span style="color:#f92672">(</span>2019-12-25 revision 647ee6f091<span style="color:#f92672">)</span> <span style="color:#f92672">[</span>x86_64-darwin19<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>which ruby <span style="color:#75715e"># 查看 ruby 位置</span>
</span></span><span style="display:flex;"><span>/Users/gua/.rbenv/shims/ruby
</span></span><span style="display:flex;"><span>which gem <span style="color:#75715e"># 查看 gem 位置</span>
</span></span><span style="display:flex;"><span>/Users/bytedance/.rbenv/shims/gem
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span>file<span style="color:#960050;background-color:#1e0010">：</span><span style="color:#66d9ef">Gemfile</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># frozen_string_literal: true</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>source <span style="color:#e6db74">&#34;https://rubygems.org&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>git_source(<span style="color:#e6db74">:github</span>) {<span style="color:#f92672">|</span>repo_name<span style="color:#f92672">|</span> <span style="color:#e6db74">&#34;https://github.com/</span><span style="color:#e6db74">#{</span>repo_name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span> }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># gem &#34;rails&#34;</span>
</span></span><span style="display:flex;"><span>gem <span style="color:#e6db74">&#34;cocoapods&#34;</span>, <span style="color:#e6db74">&#34;1.9.3&#34;</span>
</span></span></code></pre></div><p>Bundler 能够跟踪并安装所需的特定版本的 gem，以此来为 Ruby 项目提供一致的运行环境。</p>
<p>Bundler 是 Ruby 依赖管理的一根救命稻草，它可以保证你所要依赖的 gem 如你所愿地出现 在开发、测试和生产环境中。 利用 Bundler 启动项目简单到只用一条命令：<code>bundle install</code>。</p>
<blockquote>
<p>Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.</p>
</blockquote>
<blockquote>
<p>Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as <code>bundle install</code>.</p>
</blockquote>
<p>开始一个项目的工作很简单bundle install。
bundle exec pod install
bundle exec gem list
执行 bundle install 拿到别人的项目，执行这个，下载好依赖的ruby库
新项目使用顺序：</p>
<ol start="0">
<li>创建一个xcode项目</li>
<li>bundle init
首先先来初始化一个 Bundler 环境（其实就是自动创建一个 Gemfile 文件）：请参考上文Gemfile</li>
<li>bundle exec pod init 创建Podfile文件</li>
<li>bundle exec pod install 配置完pod依赖库后执行pod 初始化</li>
<li>over</li>
</ol>
<p>本文参考：<a href="https://mp.weixin.qq.com/s/fo5MNM9WpknvvehYo4V1Qw">知识小集</a> 以及自己使用过程中遇到的问题</p>

    </article>
  </div>

  
  
  

  
</body>

</html>